import { User } from "./interface";
import { createHash } from 'node:crypto'
import * as fs from 'node:fs'
import * as path from 'node:path'

const public_dir = path.join(__dirname, '../public');
export const getUserNoPassword = (user: User) => {
    return user && {
        ...user,
        password: '<hidden>',
    } as User;
}

export const md5 = (str: string) => {
    return createHash('md5').update(str).digest('hex');
}

export const generateKey = (prefix = '', suffix = '') => {
    return prefix + Date.now().toString(36) + Math.random().toString(36).substring(1) + suffix;
}
export const clientPages = (prefix: string) => {
    return {
        index: fs.readFileSync(path.join(public_dir, 'index.html'), { encoding: 'utf-8' }).replace("/client.js", `${prefix}/client.js`),
        "client.js" : fs.readFileSync(path.join(public_dir, 'client.js'), { encoding: 'utf-8' }),
        "client.js.map": fs.readFileSync(path.join(public_dir, 'client.js.map'), { encoding: 'utf-8' }),
    }
}